package com.my.blog.myblog.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.blog.myblog.entity.Log;
import com.my.blog.myblog.entity.custom.TimeCount;
import com.my.blog.myblog.service.LogService;
import com.my.blog.utils.ResultBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 *
 * @Auther: MuYun
 * @Date: 2021/9/8 21:58
 * @Description： 日志的controller
 *
 */
@RestController
@RequestMapping("/admin")
public class AdminLogController {

    @Resource
    private LogService logService;


    @GetMapping("/selectLogListByDate")
    public ResultBean<IPage<Log>> selectLogListByDate(@RequestParam(value = "startTime")String startTime,
                                                      @RequestParam(value = "endTime")String endTime,
                                                      @RequestParam(value = "page",defaultValue = "1")Integer page,
                                                      @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                                      Log log){
        ResultBean<IPage<Log>> resultBean=new ResultBean<>();
        QueryWrapper<Log> queryWrapper=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(startTime)){
            queryWrapper.lambda().gt(Log::getAddTime,startTime);
        }
        if(StrUtil.isNotEmpty(endTime)){
            queryWrapper.lambda().lt(Log::getAddTime,endTime);
        }
        if(StrUtil.isNotEmpty(log.getIp())||StrUtil.isNotEmpty(log.getUserType())||StrUtil.isNotEmpty(log.getDescription())){
            queryWrapper.lambda().like(Log::getIp,log.getIp()).
                    or().like(Log::getUserType,log.getUserType()).
                    or().like(Log::getDescription,log.getDescription());
        }
        queryWrapper.lambda().orderByDesc(Log::getAddTime);
        Page<Log> logPage = logService.page(new Page<>(page, pageSize), queryWrapper);
        if(ObjectUtil.isNotEmpty(logPage.getRecords())){
            resultBean.setData(logPage);
        }else {
            logPage.setTotal(0);
            resultBean.setData(logPage);
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    //根据时间查询用户日志
    @GetMapping("/selectUserLogByDate")
    public ResultBean<List<TimeCount>> selectUserLogByDate(Log log,
                                                           @RequestParam(value = "startTime")String startTime,
                                                           @RequestParam(value = "endTime")String endTime){
        Map<String,Object> map=new HashMap<>();
        if(ObjectUtil.isNotEmpty(log)){
            map.put("userType",log.getUserType());
        }
        if(StrUtil.isNotEmpty(startTime)){
            map.put("startTime",startTime);
        }
        if(StrUtil.isNotEmpty(endTime)){
            map.put("endTime",endTime);
        }
        List<TimeCount> timeCounts = logService.selectLogByDate(map);
        ResultBean<List<TimeCount>> resultBean=new ResultBean<>();
        if(timeCounts.size()!=0){
            resultBean.setData(timeCounts);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
